<?php
/**
 * Nordic Repository
 * 
 * LICENSE
 * 
 * The new BSD license is applied on this source-file. For further
 * information please visit http://license.nordic-dev.de/newbsd.txt
 * or send an email to andre.moelle@gmail.com.
 */

/**
 * Necessary includes.
 */
Zend_Loader::loadClass('Zend_View');

/**
 * This factory is used to create view-object with predefined paths.
 * These paths are important for many functionalities like any helper.
 * It implements some kind of factory-pattern and the singleton-pattern.
 * 
 * @category   Nordic
 * @package    Nordic_View
 * @copyright  2007 Nordic Development
 * @license    http://license.nordic-dev.de/newbsd.txt (New-BSD license)
 * @author     Andre Moelle <andre.moelle@gmail.com>
 * @version    $Id: Factory.php 3 2007-07-08 09:21:42Z andre.moelle $
 */
class Nordic_View_Factory
{
	/**
	 * Instance of the factory.
	 * 
	 * @var Nordic_View_Factory
	 */
	private static $_instance = null;
	
	/**
	 * Instance of the view-object.
	 * 
	 * @var Zend_View_Interface
	 */
	private static $_view = null;
	
	/**
	 * Returns the original view-object.
	 * 
	 * @param void
	 * @return Zend_View_Interface
	 */
	private static function _getView ()
	{
		if(self::$_view == null)
		{
			self::setView(new Zend_View());
		}
		
		return self::$_view;
	}
	
	/**
	 * Magic call-method which redirects each method-call to the view.
	 * 
	 * @param string $method called method
	 * @param array $args called args
	 * @return mixed
	 */
	public function __call ($method, $args)
	{
		$obj = self::_getView();
		
		if(!$args)
		{
			return $obj->$method();
		}
		
		return call_user_func_array(array($obj, $method), $args);
	}
	
	/**
	 * Returns the property of a view-object.
	 * 
	 * @param string $property name of the property
	 * @return mixed
	 */
	public function __get ($property)
	{
		return self::_getView()->$property;
	}
	
	/**
	 * Sets new content to a property.
	 * 
	 * @param string $property name of the property
	 * @param mixed $content new content
	 * @return void
	 */
	public function __set ($property, $content)
	{
		self::_getView()->$property = $content;
	}
	
	/**
	 * Returns the factory.
	 * 
	 * @param void
	 * @return Nordic_View_Factory
	 */
	public static function getInstance ()
	{
		if(self::$_instance == null)
		{
			self::$_instance = new self();
		}
		
		return self::$_instance;
	}
	
	/**
	 * Returns a cloned view-object.
	 * 
	 * @param void
	 * @return Zend_View_Interface
	 */
	public static function getView ()
	{
		return clone self::_getView();
	}
	
	/**
	 * Sets a new view-object.
	 * 
	 * @param Zend_View_Interface $view new view-object
	 * @return void
	 */
	public static function setView (Zend_View_Interface $view)
	{
		self::$_view = $view;
	}
}
?>